package com.itxialu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itxialu.reggie.common.BaseContext;
import com.itxialu.reggie.common.R;
import com.itxialu.reggie.entity.ShoppingCart;
import com.itxialu.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2022-11-06 12:27
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //购物车列表
    @GetMapping("/list")
    public R<List<ShoppingCart>> ShoppingList() {
        //1.取到用户ID
        long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        //添加排序
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    //添加菜品到购物车
    @PostMapping("/add")
    public R<ShoppingCart> addShopping(@RequestBody ShoppingCart shoppingCart) {//JSON形式的
        System.out.println("正在添加菜品到购物车" + shoppingCart);
        //1.设置用户ID
        long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //2.查询购物车是否已经有菜品或套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            System.out.println("传过来的是菜品");
            wrapper.eq(ShoppingCart::getDishId, dishId);
            wrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {
            System.out.println("传过来的是套餐");
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        System.out.println("看看ONE是不是NULL" + one);
        if (one != null) {//判断数据库中有没有相同的数据
            System.out.println("现在one有值，不是null");
            String dishFlavor = one.getDishFlavor();//原来购物车的口味
            String dishFlavor2 = shoppingCart.getDishFlavor();//现在购物车的口味
            //购物车已经存在
            log.info("原来购物车的口味{}", dishFlavor);
            log.info("现在购物车的口味{}", dishFlavor2);
            if ((dishFlavor == null && dishFlavor2 == null) || (dishFlavor != null && dishFlavor.equals(dishFlavor2))) {
                System.out.println("现在one有值，原购物车和传过来的口味都是null，说明是商务套餐，或且口味一样，不是null");
                Integer number = one.getNumber();//原来的数量
                one.setNumber(number + 1);//原来的数量+1
                shoppingCartService.updateById(one);
            } else {
                System.out.println("现在one有值，且口味不一样，不是null");
                shoppingCartService.save(shoppingCart);
            }

        } else {
            //购物车不存在,第一次添加
            System.out.println("购物车不存在");
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }

        return R.success(shoppingCart);
    }

    //购物车菜品删除
    @PostMapping("/sub")
    public R<String> removeShopping(@RequestBody ShoppingCart shoppingCart) {//JSON形式的
        System.out.println("购物车菜品删除...");
        Long dishId = shoppingCart.getDishId();//拿到菜品ID
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getDishId, dishId);
        int count = shoppingCartService.count(wrapper);
        System.out.println("根据dishId查到的菜品或套餐数量" + count);
        if (count == 1) {
            System.out.println("菜品或套餐数量为1的删除程序");
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
            //ShoppingCart cart = shoppingCartService.getById(wrapper);
            System.out.println("取到cart：" + cart);
            Integer number = cart.getNumber();
            System.out.println("取到number：" + number);
            if (number > 1) {
                cart.setNumber(number - 1);
                System.out.println("更新前");
                shoppingCartService.updateById(cart);
                System.out.println("更新后");
            } else {
                shoppingCartService.remove(wrapper);
            }
        }
        if (count > 1) {
            List<ShoppingCart> list = shoppingCartService.list(wrapper);
            System.out.println("多条数据，以列表形式返回" + list);
            list.stream().map((item -> {
                Long id = item.getId();
                shoppingCartService.removeById(id);
                return R.success("删除成功！");
            })).collect(Collectors.toList());

        }
        return R.success("删除成功！");
    }

    //情空购物车菜品
    @DeleteMapping("/clean")
    public R<String> deleteShopping() {
        System.out.println("开始清空购物车...");
        //1.获取用户ID
        long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        //方法一：
        shoppingCartService.remove(wrapper);
        //方法二：
        /*List<ShoppingCart> list = shoppingCartService.list(wrapper);
        list.stream().map((item ->{
            Long id = item.getId();
            shoppingCartService.removeById(id);
            return R.success("删除成功！");
        })).collect(Collectors.toList());*/
        return R.success("购物车已清空！");
    }
}
